package org.groupg.project.script;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GroovyScriptRunner {
    public static void main(String[] args) {
        String groovyScript = """
                import groovy.json.JsonSlurper
                
                // 假设输入是一个JSON字符串
                def jsonString = '''
                {
                    "company": "Moonshot AI",
                    "employees": [
                        {
                            "id": 1,
                            "name": "Alice",
                            "department": "Engineering",
                            "salary": 80000,
                            "projects": ["Project A", "Project B"]
                        },
                        {
                            "id": 2,
                            "name": "Bob",
                            "department": "Marketing",
                            "salary": 60000,
                            "projects": ["Project B"]
                        },
                        {
                            "id": 3,
                            "name": "Charlie",
                            "department": "Engineering",
                            "salary": 75000,
                            "projects": ["Project A", "Project C"]
                        }
                    ]
                }
                '''
                
                // 使用JsonSlurper解析JSON
                def jsonSlurper = new JsonSlurper()
                def companyData = jsonSlurper.parseText(jsonString)
                
                // 过滤出Engineering部门的员工
                def engineeringEmployees = companyData.employees.findAll { it.department == "Engineering" }
                
                // 按照薪资排序
                def sortedEngineeringEmployees = engineeringEmployees.sort { it.salary }
                
                // 计算平均薪资
                def totalSalary = sortedEngineeringEmployees.sum { it.salary }
                def averageSalary = totalSalary / sortedEngineeringEmployees.size()
                
                // 输出结果
                println "Company: ${companyData.company}"
                println "Engineering Department Employees:"
                sortedEngineeringEmployees.each {
                    println "Name: ${it.name}, Salary: ${it.salary}"
                }
                println "Average Salary: ${averageSalary}"
                """;

        // 创建一个复杂的JSON数据结构
        Map<String, Object> companyData = new HashMap<>();
        companyData.put("company", "Moonshot AI");

        List<Map<String, Object>> employees = new ArrayList<>();
        Map<String, Object> employee1 = new HashMap<>();
        employee1.put("id", 1);
        employee1.put("name", "Alice");
        employee1.put("department", "Engineering");
        employee1.put("salary", 80000);
        employee1.put("projects", List.of("Project A", "Project B"));
        employees.add(employee1);

        Map<String, Object> employee2 = new HashMap<>();
        employee2.put("id", 2);
        employee2.put("name", "Bob");
        employee2.put("department", "Marketing");
        employee2.put("salary", 60000);
        employee2.put("projects", List.of("Project B"));
        employees.add(employee2);

        Map<String, Object> employee3 = new HashMap<>();
        employee3.put("id", 3);
        employee3.put("name", "Charlie");
        employee3.put("department", "Engineering");
        employee3.put("salary", 75000);
        employee3.put("projects", List.of("Project A", "Project C"));
        employees.add(employee3);

        companyData.put("employees", employees);

        // 将复杂数据结构转换为JSON字符串
        String jsonString = new groovy.json.JsonBuilder(companyData).toPrettyString();

        // 创建Groovy脚本绑定
        Binding binding = new Binding();
        binding.setVariable("jsonString", jsonString);

        // 创建GroovyShell并执行脚本
        GroovyShell shell = new GroovyShell(binding);
        try {
            Script script = shell.parse(groovyScript);
            script.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}